// use std::io::{self, Write};
use std::error::Error;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;

#[test]
fn stdout_demo() {
    println!("hello there!");
    println!("format {} arguments", "some");
}

#[test]
fn file_demo() {
    let path = Path::new("/etc/hosts");
    let display = path.display();

    let mut file = match File::open(&path) {
        Err(why) => panic!("couldn't open {}: {}", display, Error::description(&why)),
        Ok(file) => file,
    };

    let mut body = String::new();

    match file.read_to_string(&mut body) {
        Err(why) => panic!("read {} failed: {}", display, Error::description(&why)),
        Ok(_) => println!("{} contains {}", display, body),
    }
}

#[test]
fn file_write_demo() {
    let body = "write me to a file please";
    let path = Path::new("/tmp/rust_body.txt");
    let display = path.display();

    // 用只写模式打开一个文件，并返回 `io::Result<File>` 类型
    let mut file = match File::create(&path) {
        Err(why) => panic!("couldn't create {}: {}", display, Error::description(&why)),
        Ok(file) => file,
    };

    // 写入 `LOREM_IPSUM` 字符串到文件中, 并返回 `io::Result<()>` 类型
    match file.write_all(body.as_bytes()) {
        Err(why) => panic!(
            "couldn't write to {}: {}",
            display,
            Error::description(&why)
        ),
        Ok(_) => println!("successfully wrote to {}", display),
    }
}
